$(function() {
	load();	
	$("#g1").focus(); 
	$("#g1").select(); 
	
	selectWaitTc();
	setIntervalDemo();
	
    var product = $("#product").val();
	if(product == "" || product == undefined || product == null) {
		return;
	}else {
    	selectNum();
    	selectYearNum();
	}
})

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

    
 	var flag = 0;
 	//input 开始数字
 	var inpK = 1;
 	//input 结束数字
 	var inpJ = 12;
	function keyDown(e){  
	    //IE内核浏览器  
	     if (navigator.appName == 'Microsoft Internet Explorer'){  
	           var keycode = event.keyCode;  
	           var realkey = String.fromCharCode(event.keyCode);  
	     }else {//非IE内核浏览器  
	           var keycode = e.which;  
	           var realkey = String.fromCharCode(e.which);  
	     }  

	     //监听Ctrl键  
	     if(keycode==13){ 
	  	   if($("#icode").val() != null && $("#icode").val() != "" && $("#icode").val() != undefined) {
	  		   var icode =  $("#icode").val();
	  			$("#icode").val(icode+",");
	  		   return;
	  	   }
	  	   
	  	   if(flag == 0) {
	  		 var taiche=$.trim($("#g1").val());
			 if(taiche == "" || taiche == undefined || taiche == null) {
				 return;
		     }else {
	   			//查询此台车是否可用
	   			$.ajax({
			    	type:'post',
			    	async:false, 
			    	url:'/solid/selectTc.do',
			    	data: {"cremark":taiche},
			    	success:function(data){
			    		if(data == -1){
			    			toastr.warning("对不起，此台车已满！");
			    			$("#g1").focus(); 
			    	        $("#g1").select();  
			    		}else if(data == -2) {
			    			toastr.warning("对不起，此台车已开始固化！");
			    			$("#g1").focus(); 
			    	        $("#g1").select();  
			    		}else {
			    			toastr.warning("此台车最多还能绑定"+data+"支组件");
			    			$("#c1").focus(); 
			    	        $("#c1").select();  
			    	        inpJ = data;
			    	        flag = 1;
			    		}
			    	}
	   			});
		   	  }
	  	   }else {
	  		 getId();
	  	   }
	    }
     }  
	//按键事件
	document.onkeydown=keyDown;
	
	function getId(){ 
		var code=$.trim($("#c1").val());
		var mid = inpK;
			//alert(mid+"------");
		 if(code.length != 16 || code.substring(1,3) != "PS"){
			toastr.warning("请扫码正确的膜管编号！");
			mid = parseInt(mid) - parseInt(1);
	        $("#c1").select();
			return;
		 }else {
			 
			 $.ajax({
		    	type:'post',
		    	async:false, 
		    	url:'/solid/selectM.do',
		    	data: {"stube":code},
		    	success:function(data){
		    		if(data == -1 ){
		    			toastr.warning("对不起，此条码还未离心，不能进行固化！");
		    		}else if(data>0){
		    			toastr.warning("对不起，数据已存在，请重新扫码！");
		    			mid = parseInt(mid) - parseInt(1);
		    	        $("#c1").select();
		    			return;
		    		}else{
		    			
		    			for (var i = 1; i<mid; i++) {
		    				//alert("i="+i);
		    				//alert("mid="+mid);
		    				//页面已存在的每一个膜管的码
		    				var stube = document.getElementById("m"+i).innerHTML;
		    				//与c1中的码做比较
		    				if(code == stube ){
		    					toastr.warning("对不起，当前页面已有，请重新扫码！");
		    					mid = parseInt(mid) - parseInt(1);
		    					$("#c1").select();
		    					return;
		    				}
		    			}
		    			
		    			if(parseInt(inpK) < parseInt(inpJ)+1){
		    				
		    				$("#m"+inpK).text(code);
		    				var time = new Date().Format("yyyy-MM-dd HH:mm:ss");  
		    				$("#t"+inpK).text(time);
	    					inpK = inpK+1;
	    					//alert("inpK"+inpK);
	    					//alert("inpJ"+inpJ);
	    					$("#c1").focus(); 
    						$("#c1").select(); 
	    				}else{
	    					//逻辑：这是最后一个码,把光标定位在台车
	    					layer.confirm('台车已满，确认提交吗？', 
	    						{ 
	    						success:function(){
	    							this.enterEsc = function (event) {
	    								 if(ev.keyCode==13) { 
	    								        $(this).trigger("click");
	    										return false
	    								    }  
	    							};
	    							$(document).on('keydown', this.enterEsc); //监听键盘事件，关闭层
	    						},
	    						end:function(){
	    							$(document).off('keydown',this.enterEsc); //解除键盘关闭事件
	    						}
	    					},
	    							
	    							
	    						function(index){
	    						layer.closeAll('dialog');
	    						var taiche=$.trim($("#g1").val());
	    						//判断页面是否有空的
	    						if(taiche == "" || taiche == undefined || taiche == null) {
	    							return;
	    						}else {
	    							for (var i = 1; i<inpK; i++) {
	    							 var stube = $("#m"+i).text();
	    							 var time = $("#t"+i).text();
	    							//批量提交
	    							 $.ajax({
	    						    	type:'post',
	    						    	async:true, 
	    						    	url:'/solid/save.do',
	    						    	data: {"cremark":taiche,"stube":stube,"cremark1":time},
	    						    	success:function(data){
	    						    		if(data>0){
	    						    		}else{
	    						    			toastr.warning("绑定失败！");
	    						    			return;
	    						    		}
	    						    	}
	    						    });
	    						  }
	    						
	    							toastr.success("绑定成功！");
	    							selectWaitTc();
	    							$("#g1").val("");
	    							$("#c1").val("");
	    							for (var i = 0; i < 13; i++) {
	    								$("#m"+i).text("");
	    								$("#t"+i).text("");
	    							}
	    							inpK = 1;
	    							//input 结束数字
	    							inpJ = 12;
	    							//input 开始id
	    							id = "g1";
	    							flag = 0;
	    							$("#g1").focus(); 
	    							$("#g1").select(); 
	    						}
	    		        	})
	    				} //表格循环结束
		    		}
		    	}
		    })
		 }
	}
	
	//查询待绑定台车列表
	function selectWaitTc() {
		$.ajax({
	    	type:'post',
	    	dataType: 'json',
	    	url:'/solid/selectWaitTc.do',
	    	success:function(data){
	    		for(var i = 1; i < data.length+1; i++) {
    				//台车
		    		$("#w"+i).html(data[i-1]);
    			}
	    		if(10> data.length) {
    				var size = data.length;
    				var x = 10 - size
    				for(var i = 0; i < x; i++) {
    					var j = 10-i;
	    				//台车
    					$("#w"+j).html("");
	    			}
    			}
	    	}
	    });
	}
	
	var index;
	function pei() {
		$("#searchName").val("");
		$("#serch").click();
		index = layer.open({
			type: 1 					//Page层类型
	  		,area: ['750px', '600px'] 	//宽  高
	  		,title: '选择配件'
	  		,shade: 0.6 				//遮罩透明度
	  		,maxmin: true 				//允许全屏最小化
	  		,anim: 1 					//0-6的动画形式，-1不开启
	  		,content: $("#peiDivID")
	  		,end: function(){
	  			$("#peiDivID").hide();
	  		} 
		});
	}
	
	
	/*AGV拉动*/
	function agv() {
		var tc = $("#g1").val();
		$.ajax({
	    	type:'post',
	    	url:'/solidAgv.do',
	    	data: {"tc":tc},
	    	success:function(data){
	    		if(data == 1) {
	    			toastr.success("指令发送成功，等待AGV响应!");
	    		}else {
	    			toastr.err("AGV拉动失败！");
	    		}
	    	}
	    });
	}
	
	 //定时器
    function setIntervalDemo(){
        setTimeoutName = setInterval(function(){
        	
        	 $.ajax({
   			  	async:false, 
   		    	type:'post',
   		    	url:'/getBunding.do',
   		    	success:function(data){
   		    		if(data == 1) {
   		    			//toastr.success("agv已接收指令");
   		    			$.ajax({
   		    			  	async:false, 
   		    		    	type:'post',
   		    		    	url:'/setBunding.do',
   		    		    	success:function(data){
   		    		    	}
   		    			});
   		    			selectWaitTc();
   		    		}
   		    	}
        	 });
        },1000);
    }

